package com.backend.securitiessystem.aop;

import com.backend.securitiessystem.crawler.properties.CrawlerProperties;
import com.backend.securitiessystem.dao.ICrawlerLogDao;
import com.backend.securitiessystem.pojo.CrawlerLog;

import lombok.extern.slf4j.Slf4j;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

import java.util.Date;

/**
 * 爬虫日志 AOP
 *
 * @author TYF
 * @date 2024-05-21 03:15:51
 */
@Component
@Aspect
@Slf4j
public class CrawledLogAspect {

    @Resource
    private ICrawlerLogDao crawlerLogDao;

    @Resource
    private CrawlerProperties crawlerProperties;

    //定义切点
    @Pointcut(value = "execution(* com.backend.securitiessystem.task.FileCleanTask.*())")
    public void aopWebLog() {
    }

    //使用环绕通知
    @Around("aopWebLog()")
    public Object insertCrawledLog(ProceedingJoinPoint pjp) throws Throwable {
        Date start = new Date();

        // 调用方法
        pjp.proceed();

        // 开始记录日志
        // 获取方法签名
        MethodSignature signature = (MethodSignature) pjp.getSignature();

        // 获取方法名
        String methodName = signature.getMethod().getName();

        // 根据不同方法名记录不同日志
        CrawlerLog crawlerLog = getCrawlerLog(methodName, start);
        crawlerLogDao.insert(crawlerLog);

        return null;
    }

    /**
     * 插入爬虫日志
     *
     * @author tuyufeng
     * @date 2024/05/21
     */
    private CrawlerLog getCrawlerLog(String methodName, Date start) {

        CrawlerLog crawlerLog = new CrawlerLog();

        // 获取中国指数日志
        switch (methodName) {
            case "updateChinaIndexTimingTask":
                crawlerLog.setUrl(crawlerProperties.getAsianChinaStockMarketsUrl());
                crawlerLog.setState("获取中国股市指数");
                crawlerLog.setTableId(1);
                break;
            case "updateNewShareTimingTask":
                crawlerLog.setUrl(crawlerProperties.getNewShareUrl());
                crawlerLog.setState("获取新股信息");
                crawlerLog.setTableId(2);
                break;
            case "updateFlashTimingTask":
                crawlerLog.setUrl(crawlerProperties.getSecuritiesNewsUrl());
                crawlerLog.setState("获取快讯");
                crawlerLog.setTableId(3);
                break;
            case "updateCapitalFlowTimingTask":
                crawlerLog.setUrl(crawlerProperties.getCapitalFlowUrl());
                crawlerLog.setState("获取港股通资金流信息");
                crawlerLog.setTableId(4);
                break;
            case "updateRiseFallTimingTask":
                crawlerLog.setUrl(crawlerProperties.getRiseFallUrl());
                crawlerLog.setState("获取板块跌涨信息");
                crawlerLog.setTableId(5);
                break;
            default:{}
        }

        crawlerLog.setStartTime(start);
        crawlerLog.setEndTime(new Date());
        return crawlerLog;
    }

}
